#include "dialog.h"
#include "ui_dialog.h"
#include "mainwindow.h"

#include <QDebug>
#include <QFile>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    qDebug() << this->parentWidget();
    ui->pushButton_sure->setEnabled(false);
}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_pushButton_sure_clicked()
{
    MainWindow *par = static_cast<MainWindow *>(this->parentWidget());
    par->setSignupname(ui->lineEdit_username->text());
    par->setSignpasswd(ui->lineEdit_passwd->text());
    par->setSignupquestion(ui->lineEdit_question->text());
    par->setTobehandle(true);

    par->write();

    this->close();
}


void Dialog::on_pushButton_cancel_clicked()
{
    this->close();
}


void Dialog::on_lineEdit_username_textChanged(const QString &arg)
{
//    qDebug() << arg;
    MainWindow *par = static_cast<MainWindow *>(this->parentWidget());
    if(par->filecontains(QString("username=%1\n").arg(arg)) ||
                        par->filecontains(QString("username=%1\r\n").arg(arg)))
    {
        ui->pushButton_sure->setEnabled(false);
    }
    else
    {
        ui->pushButton_sure->setEnabled(true);
    }
}

